//package cn.fl.login.controller.filter;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
//import javax.servlet.Filter;
//import javax.servlet.FilterChain;
//import javax.servlet.FilterConfig;
//import javax.servlet.ServletException;
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.Subject;
//
//import cmm.user.facade.system.dto.SysUsrDTO;
//import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
//import cmm.web.core.framework.filter.ParameterRequestWrapper;
//import cn.cmm.base.framework.constant.BaseConstant;
//import cn.cmm.base.framework.utils.CollectionUtils;
//import cn.fl.framework.base.constant.WebBaseConstant;
//import cn.fl.framework.base.context.CurrentThreadContext;
//
//public class LoginFilter implements Filter {
//
//	@Override
//	public void destroy() {
//	}
//
//	@Override
//	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//			throws IOException, ServletException {
//		 //对request和response进行一些预处理
//		request.setCharacterEncoding("UTF-8");
//		response.setCharacterEncoding("UTF-8");
//		response.setContentType("text/html;charset=UTF-8");
//		Subject subject = SecurityUtils.getSubject();
//		//HttpServletRequest httpRequest = (HttpServletRequest)request;
//		//HttpServletResponse httpResponse = (HttpServletResponse)response;
//		Session session = subject.getSession(true);
//		SysUsrDTO sysUsrDTO = (SysUsrDTO)session.getAttribute(CurrentThreadContext.CURRENT_USER_INFO);
//		if(subject.isAuthenticated() && null != sysUsrDTO){
//			CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_USER_ID, sysUsrDTO.getId());
//			CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_USER_NAME, sysUsrDTO.getRefRlNm());
//			CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_USER_INFO, sysUsrDTO);
//			/*session.setAttribute(CurrentThreadContext.CURRENT_USER_ID, sysUsrDTO.getId());
//			session.setAttribute(CurrentThreadContext.CURRENT_USER_NAME, sysUsrDTO.getRlNm());
//			session.setAttribute(CurrentThreadContext.CURRENT_USER_INFO, sysUsrDTO);*/
//
//			List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = sysUsrDTO.getSysUsrFunAuthRDTOList();
//			//获取默认的组织架构和角色
//			if(CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOList)){
//				//标识是否是切换了当前角色
//				Boolean isChange = (Boolean)session.getAttribute(CurrentThreadContext.CURRENT_CHANGE_ROLE_FLAG);
//				if(null == isChange || isChange == false){
//					SysUsrFunAuthRDTO sysUsrFunAuthRDTO = null;
//					for (SysUsrFunAuthRDTO sfa : sysUsrFunAuthRDTOList) {
//						if(Integer.valueOf(BaseConstant.IS_YESNO_YES).equals(sfa.getRoleIsVis())){
//							sysUsrFunAuthRDTO = sfa;
//	    					break;
//	    				}
//					}
//					if(null != sysUsrFunAuthRDTO){
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_CD, sysUsrFunAuthRDTO.getOrgCd());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_ID, sysUsrFunAuthRDTO.getRoleId());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_NM, sysUsrFunAuthRDTO.getRoleNm());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_NM, sysUsrFunAuthRDTO.getOrgNm());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_ID, sysUsrFunAuthRDTO.getOrgId());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_SHT_NM, sysUsrFunAuthRDTO.getOrgShtNm());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_DISP_NM, sysUsrFunAuthRDTO.getOrgDispNm());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_TYP_CD, sysUsrFunAuthRDTO.getOrgTypCd());
//						/*session.setAttribute(CurrentThreadContext.CURRENT_ORG_CD, sysUsrFunAuthRDTO.getOrgCd());
//						session.setAttribute(CurrentThreadContext.CURRENT_ROLE_ID, sysUsrFunAuthRDTO.getRoleId());
//						session.setAttribute(CurrentThreadContext.CURRENT_ROLE_NM, sysUsrFunAuthRDTO.getRoleNm());
//						session.setAttribute(CurrentThreadContext.CURRENT_ORG_NM, sysUsrFunAuthRDTO.getOrgNm());*/
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD, sysUsrFunAuthRDTO.getRgnDivCd());
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, sysUsrFunAuthRDTO.getRgnDivCdNm());
//
//						List<Long> roleIds = new ArrayList<>();
//						//要把所有角色赋值给线程变量,给前端使用
//						for (SysUsrFunAuthRDTO sufard : sysUsrFunAuthRDTOList) {
//							roleIds.add(sufard.getRoleId());
//						}
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_GRP_ID, roleIds);
//						session.setAttribute(CurrentThreadContext.CURRENT_ROLE_GRP_ID, roleIds);
//						session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD, sysUsrFunAuthRDTO.getRgnDivCd());
//						session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, sysUsrFunAuthRDTO.getRgnDivCdNm());
//
//						CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_RES_POWER_URL, session.getAttribute(CurrentThreadContext.CURRENT_RES_POWER_URL));
//
//					}
//				}else{
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_CD, session.getAttribute(CurrentThreadContext.CURRENT_ORG_CD));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_ID, session.getAttribute(CurrentThreadContext.CURRENT_ROLE_ID));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_NM, session.getAttribute(CurrentThreadContext.CURRENT_ROLE_NM));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_NM, session.getAttribute(CurrentThreadContext.CURRENT_ORG_NM));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_ID, session.getAttribute(CurrentThreadContext.CURRENT_ORG_ID));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_SHT_NM, session.getAttribute(CurrentThreadContext.CURRENT_ORG_SHT_NM));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_DISP_NM, session.getAttribute(CurrentThreadContext.CURRENT_ORG_DISP_NM));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_GRP_ID, session.getAttribute(CurrentThreadContext.CURRENT_ROLE_GRP_ID));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_TYP_CD, session.getAttribute(CurrentThreadContext.CURRENT_ORG_TYP_CD));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD, session.getAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, session.getAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM));
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_RES_POWER_URL, session.getAttribute(CurrentThreadContext.CURRENT_RES_POWER_URL));
//
//				}
//
//			}else{
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_CD, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_ID, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_ID, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_SHT_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_DISP_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_GRP_ID, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_TYP_CD, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_GRP_ID, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_RES_POWER_URL, null);
//
//				/*session.setAttribute(CurrentThreadContext.CURRENT_ORG_CD, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_ID, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_NM, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_NM, null);*/
//			}
//			session.setTimeout(WebBaseConstant.SYS_SESSION_TIME);//毫秒
//
//			//处理空格
//			ParameterRequestWrapper parameterRequestWrapper = new ParameterRequestWrapper((HttpServletRequest)request);
//			chain.doFilter(parameterRequestWrapper, response);  //让目标资源执行，放行
//		}else{
//			subject.logout();
//			chain.doFilter(request, response);  //让目标资源执行，放行
//		}
//	}
//
//	@Override
//	public void init(FilterConfig arg0) throws ServletException {}
//
//}
